﻿<cfcomponent>
<cfscript>
//会员竞价/代理出价接口

DB = 'chinalaohu';
dbt = 'auction';

//加价幅度
function addpfunc(jg){
	if (jg lt 50){
		addp = 1;
	}else if(jg gte 50 and jg lt 100){
		addp = 5;
	}
	else if(jg gte 100 and jg lt 500){
		addp = 10;
	}
	else if(jg gte 500 and jg lt 1000){
		addp = 20;
	}
	else if(jg gte 1000 and jg lt 2000){
		addp = 50;
	}
	else if(jg gte 2000 and jg lt 5000){
		addp = 100;
	}
	else if(jg gte 5000 and jg lt 10000){
		addp = 200;
	}
	else if(jg gte 10000 and jg lt 20000){
		addp = 500;
	}
	else if(jg gte 20000 and jg lt 50000){
		addp = 500;
	}
	else if(jg gte 50000 and jg lt 200000){
		addp = 1000;
	}
	else {
		addp = 2000;
	}
	return addp;
}
</cfscript>

<!--- 用户出价 --->
<cffunction name="userbid" access="remote" returntype="any" returnFormat="json">
	<cfscript>
		rs = {"success":"0"};
	</cfscript>
	<cfif isdefined('aid') AND isdefined('UUID') AND isdefined('myPrice') AND isdefined('key')>
	<cfscript>
		newkey = hash(aid & UUID & myPrice & 'laohu123','MD5');
	</cfscript>
	<cfif key neq newkey>
		<cfscript>
		//KEY不正确
		rs = {"success":"0","errCode":"-1","m":"k"};
		</cfscript>
	<cfelse>
		<!---是否存在currentprice--->
		<cfquery datasource="#DB#" name="aidprice">
			select lastprice from auction where aid = #aid#
		</cfquery>
		<cfscript>
			isOrigin = 1;
			if(aidprice.recordcount gt 0){
				currentprice = aidprice.lastprice;
				if(aidprice.lastprice gt 0){
					isOrigin = 0;
				}
			}else{
				currentprice = 0;
			}
			//没找到拍品记录，当前价格为0
			rs = {"success":"0","price":currentprice,"errCode":"-1","msg":"预制返回值"};
		</cfscript>
		<cfif len(UUID) lt 32>
			<!---"msg":"错误的ID",--->
			<cfset rs = {"success":"0","price":currentprice,"msg":"错误的ID"}>
		<cfelse>
			<!---根据UUID查找ID--->
			<cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" />
			<cfif uuidgetuid.recordcount neq 1>
				<!---,"msg":"用户ID错误"--->
				<cfset rs = {"success":"0","price":currentprice,"errCode":"0","msg":"获取用户失败"}>
			<cfelse>
				<!---拍卖额度--->
				<cfinvoke component="app.cfc.credit" method="userCreditLimit" returnvariable="userCreditLimit" aid="#aid#" uid="#uuidgetuid.uid#" />
				<!--- 当前最高代理价信息 --->
				<cfquery datasource="#DB#" name="isbidproxy">
					SELECT bidproxy,uid,is_bid from auction_userbidproxy where aid=#aid# order by xid desc limit 1
				</cfquery>
				<!---是否最高出价人--->
				<cfquery datasource="#DB#" name="islastbid">
					select bid,uid from auction_userlastbid where aid = #aid# and is_bid = 1
				</cfquery>
				<cfscript>
					//此处为客户端计算调整
					fixPrice = 0;
					if(isbidproxy.recordcount gt 0 and isbidproxy.is_bid eq 1 and isbidproxy.uid eq uuidgetuid.uid){
						fixPrice = isbidproxy.bidproxy;
					}else if(islastbid.recordcount gt 0 and islastbid.uid eq uuidgetuid.uid){
						fixPrice = islastbid.bid;
					}
				</cfscript>
				<cfif myPrice gt (userCreditLimit+fixPrice)>
					<!---,"msg":"额度不足"--->
					<cfset rs = {"success":"0","price":currentprice,"isOrigin":isOrigin,"errCode":"-1","msg":"额度不足"}>
				<cfelse>
					<!---获取当前价  atag 1竞拍中--->
					<cfquery datasource="#DB#" name="getlastprice">
						select lastprice,starttime,endtime,bprice from auction where aid = #aid# and is_delete = 0 and atag = 1
					</cfquery>
					<cfif getlastprice.recordcount eq 0>
						<!---,"msg":"未找到拍品"--->
						<cfset rs = {"success":"0","price":currentprice,"isOrigin":isOrigin,"errCode":"-1","msg":"未找到拍品"}>
					<cfelseif getlastprice.starttime gt now() or getlastprice.endtime lt now()>
						<!---,"msg":"不在竞拍时间段"--->
						<cfset rs = {"success":"0","price":currentprice,"isOrigin":isOrigin,"errCode":"-1","msg":"不在竞拍时间段"}>
					<cfelse>
						<cfscript>
							//最小出价
							firstPrice = getlastprice.lastprice + addpfunc(getlastprice.lastprice);
						</cfscript>
						<!---有人出价?--->
						<cfquery datasource="#DB#" name="isbid">
							select bid from auction_userlastbid where aid = #aid#
						</cfquery>
						<!---是否领先--->
						<cfquery datasource="#DB#" name="uidisLead">
							select uid,is_bid from auction_userlastbid where aid = #aid# AND uid = #uuidgetuid.uid#
						</cfquery>
						<!---代理价--->
						<cfquery datasource="#DB#" name="haveproxy">
							SELECT bidproxy,uid from auction_userbidproxy where aid = #aid# and uid=#uuidgetuid.uid# order by xid desc limit 1
						</cfquery>
						<cfscript>
							isLead = 0;			//是否领先
							isProxy = 0;		//是否为代理价
							proxyPrice = 0;		//代理价格
							credit = 0;			//出价额度
						</cfscript>
						<!--- 用户出价额度 --->
						<cfinvoke component="app.cfc.credit" method="userCreditLimit" returnvariable="userCreditLimit" aid="#aid#" uid="#uuidgetuid.uid#" />
						<cfif myPrice lt firstPrice>
							<!--- 如果我出的价格小于当前价格 --->
							<cfscript>
								if (uidisLead.recordcount gt 0){
									isLead = uidisLead.is_bid;
								}
								if (haveproxy.recordcount gt 0){
									isProxy = 1;
									proxyPrice = haveproxy.bidproxy;
								}
								//rs = {"price":currentprice};
								//,"msg":"出价太少"
								rs = {"success":"1","price":currentprice,"isLead":isLead,"isProxy":isProxy,"proxyPrice":proxyPrice,"credit":userCreditLimit,"isOrigin":isOrigin,"errCode":"-1","msg":"出价太少"};
							</cfscript>
						<cfelse>
							<!--- 我出的价格大于当前价格 --->
                            <!--- 我出的价格小于底价 --->
                            <cfif myPrice lt getlastprice.bprice>
							  <cfscript>
                                  b = myPrice;
                                  //更新最高出价
                                  currentprice = createObject("component",'bid').newbid_1(uid=uuidgetuid.uid,nick=uuidgetuid.mobile,bid=b);
                              </cfscript>
							<!--- 我出的价格大于等于底价 --->
                            <cfelse>
                              <cfscript>
                                  //是否有人又出了最高价
                                  if (isbid.recordcount eq 0){
                                      //目前没人出价
                                      b = firstPrice;
                                      //更新最高出价
                                      currentprice = createObject("component",'bid').newbid(uid=uuidgetuid.uid,nick=uuidgetuid.mobile,bid=b);
                                      //如果我出的价格仍然大于最新的价格
                                      if (myPrice gt firstPrice){
                                          ip = myPrice;
                                          //更新最高代理价
                                          currentproxy = createObject("component",'bid').newbidproxy(uid=uuidgetuid.uid,limittime=getlastprice.endtime,bidproxy=ip);
                                      }
                                  }else{
                                      //我出的就是当前最高价
                                      if (islastbid.uid eq uuidgetuid.uid){
                                          //是否设代理
                                          if (isbidproxy.recordcount eq 0){
                                              //没设置过代理
                                              ip = myPrice;
                                              currentproxy = createObject("component",'bid').newbidproxy(uid=uuidgetuid.uid,limittime=getlastprice.endtime,bidproxy=ip);
                                          }else{
                                              //设置了代理
                                              up = myPrice;
                                              //pid = uuidgetuid.uid;
                                              currentproxy = createObject("component",'bid').modbidproxy(uid=uuidgetuid.uid,limittime=getlastprice.endtime,bidproxy=up);
                                          }									
                                      } else {
                                          //有人出了最高价，但不是我
                                          //是否设代理
                                          if(isbidproxy.recordcount eq 0){
                                              //没有设置代理
                                              b = firstPrice;
                                              currentprice = createObject("component",'bid').newbid(uid=uuidgetuid.uid,nick=uuidgetuid.mobile,bid=b);
                                              if (myPrice gt firstPrice){
                                                  ip = myPrice;
                                                  currentproxy = createObject("component",'bid').newbidproxy(uid=uuidgetuid.uid,limittime=getlastprice.endtime,bidproxy=ip);
                                              }
                                          }else{
                                              //有人设置了代理
                                              omobile = createObject("component",'bid').getuidmobile(uid=isbidproxy.uid).mobile;
                                              //代理价小于我出的价格
                                              if (isbidproxy.bidproxy lt myPrice){
                                                  //旧代理人出价
                                                  ob = isbidproxy.bidproxy;
                                                  
                                                  //我出的价格超过旧代理人出的代理价
                                                  //需要增加一条与旧代理人代理价相同的竞价记录
                                                  //然后被旧代理人出最高的代理价超过
                                                  //最后我再出最高的价格
                                                  currentprice = createObject("component",'bid').newbid(uid=uuidgetuid.uid,nick=uuidgetuid.mobile,bid=ob);
                                                  createObject("component","bid").delproxy();
  
                                                  //wirtedump(omobile);
                                                  currentprice = createObject("component",'bid').newbid(uid=isbidproxy.uid,nick=omobile,bid=ob);
                                                  createObject("component","bid").delproxy();
                                                  //delproxy = 1;
                                                  b = isbidproxy.bidproxy + addpfunc(isbidproxy.bidproxy);
                                                  currentprice = createObject("component",'bid').newbid(uid=uuidgetuid.uid,nick=uuidgetuid.mobile,bid=b);
                                                  if (myPrice gt b){
                                                      ip = myPrice;
                                                      currentproxy = createObject("component",'bid').newbidproxy(uid=uuidgetuid.uid,limittime=getlastprice.endtime,bidproxy=ip);
                                                  }
                                              } else {
                                                  //代理价大于当前出价
                                                  b = myPrice;
                                                  currentprice = createObject("component",'bid').newbid(uid=uuidgetuid.uid,nick=uuidgetuid.mobile,bid=b);
                                                  ob = myPrice;
                                                  currentprice = createObject("component",'bid').newbid(uid=isbidproxy.uid,nick=omobile,bid=ob);
                                                  if (isbidproxy.bidproxy eq myPrice){
                                                      //delproxy = 1;
                                                      createObject("component","bid").delproxy();
                                                  }
                                              }
                                          }											
                                      }
                                  }
                              </cfscript>
                            </cfif>
							<!---再次检测--->
							<!---是否领先--->
							<cfquery datasource="#DB#" name="uidisLead2">
								select uid,is_bid
								from auction_userlastbid
								where aid = #aid# AND uid = #uuidgetuid.uid#
							</cfquery>
							<!---最新代理价--->
							<cfquery datasource="#DB#" name="haveproxy2">
								SELECT bidproxy,uid
								from auction_userbidproxy
								where aid = #aid# and uid=#uuidgetuid.uid# and limittime > #now()#
							</cfquery>
							<cfscript>
								if (uidisLead2.recordcount gt 0){
									isLead = uidisLead2.is_bid;
								}
								if (haveproxy2.recordcount gt 0){
									isProxy = 1;
									proxyPrice = haveproxy2.bidproxy;
								}
								if (isLead eq 0 AND myPrice lt getlastprice.bprice){
									msg = "当前出价没有超过该产品的底价！";
								}else if (isLead eq 0 AND myPrice gte getlastprice.bprice){
									msg = "未领先，有用户设置了更高代理价！";
								}else{
									msg = "竞价成功！";
								}
							</cfscript>
							<cfinvoke component="app.cfc.credit" method="userCreditLimit" returnvariable="userCreditLimit" aid="#aid#" uid="#uuidgetuid.uid#" />
							<cfscript>
								if(isLead eq 1){
									//在客户端中，可用额度减去了当前价，或者代理价（不该减去），在此加回来，避免客户端显示错误
									if(isProxy eq 1){
										userCreditLimit = userCreditLimit+proxyPrice;
									}else{
										userCreditLimit = userCreditLimit+currentprice;
									}
								}
								rs = {"success":"1","price":Numberformat(currentprice,'_.__'),"isLead":isLead,"isProxy":isProxy,"proxyPrice":Numberformat(proxyPrice,'_.__'),"credit":Numberformat(userCreditLimit,'_.__'),"isOrigin":"0","msg":msg};
							</cfscript>
							<!---拍品代理价所有人--->
						</cfif>
					</cfif>
				</cfif>      
			</cfif> 
		</cfif>
	</cfif>
    
    
    
    
    
  <cfelse>
  	<cfscript>
		//参数不足
		rs = {"success":"0","errCode":"-1","msg":"参数不足"};
		</cfscript>

    
  </cfif>
	<cfscript>
  rs = createObject("component",'cfjson').encode(rs);
  return rs;
  </cfscript>
</cffunction>

<!--- 出价 --->
<cffunction name="newbid" access="public" returntype="numeric">
	<cfinvoke method="setisbid0" />
	<cfquery datasource="#DB#">
		insert into auction_userbid(aid,uid,nick,bid,bid_dt)
		value(#aid#,#uid#,'#nick#',#bid#,#now()#)
	</cfquery>
	<!---更新最后出价记录--->
	<cfquery datasource="#DB#" name="checkuidbid">
		select aulbid from auction_userlastbid where aid = #aid# and uid = #uid#
	</cfquery>
	<cfif checkuidbid.recordcount eq 0>
		<cfquery datasource="#DB#">
			INSERT INTO auction_userlastbid(aid,uid,bid,bid_dt)
			VALUE(#aid#,#uid#,#bid#,#now()#)
		</cfquery>                
	<cfelse>
		<cfquery datasource="#DB#">
			UPDATE auction_userlastbid SET
			bid = #bid#,
			bid_dt = #now()#,
			is_bid = 1
			WHERE aid = #aid# and uid = #uid#
		</cfquery>
	</cfif>
	<cfquery datasource="#DB#">
		update auction set lastprice = #bid#,bids = bids + 1,lasttime = #now()# WHERE aid = #aid#
	</cfquery>
	<cfreturn bid>
</cffunction>

<!---出价低于底价时 出价 --->
<cffunction name="newbid_1" access="public" returntype="numeric">
	<cfinvoke method="setisbid0" />
	<cfquery datasource="#DB#">
		insert into auction_userbid(aid,uid,nick,bid,bid_dt,is_bid)
		value(#aid#,#uid#,'#nick#',#bid#,#now()#,0)
	</cfquery>
	<!---更新最后出价记录--->
	<cfquery datasource="#DB#" name="checkuidbid">
		select aulbid from auction_userlastbid where aid = #aid# and uid = #uid#
	</cfquery>
	<cfif checkuidbid.recordcount eq 0>
		<cfquery datasource="#DB#">
			INSERT INTO auction_userlastbid(aid,uid,bid,bid_dt,is_bid)
			VALUE(#aid#,#uid#,#bid#,#now()#,0)
		</cfquery>                
	<cfelse>
		<cfquery datasource="#DB#">
			UPDATE auction_userlastbid SET
			bid = #bid#,
			bid_dt = #now()#,
			is_bid = 0
			WHERE aid = #aid# and uid = #uid#
		</cfquery>
	</cfif>
	<cfquery datasource="#DB#">
		update auction set lastprice = #bid#,bids = bids + 1,lasttime = #now()# WHERE aid = #aid#
	</cfquery>
	<cfreturn bid>
</cffunction>

<!---出代理价--->
<cffunction name="newbidproxy" access="public" returntype="numeric">
	<cfquery datasource="#DB#">
		delete from auction_userbidproxy where aid = #aid#
	</cfquery>
	<cfquery datasource="#DB#">
		insert into auction_userbidproxy(aid,uid,bidproxy,bidproxy_dt,limittime)
		VALUE(#aid#,#uid#,#bidproxy#,#now()#,'#limittime#')
	</cfquery>
	<cfreturn bidproxy>
</cffunction>

<!---更新代理价--->
<cffunction name="modbidproxy" access="public" returntype="numeric">
	<cfquery datasource="#DB#">
		update auction_userbidproxy set
		uid = #uid#,
		bidproxy = #bidproxy#,
		limittime = "#limittime#"
		WHERE aid = #aid#
	</cfquery>
	<cfreturn bidproxy>
</cffunction>

<!---删除代理--->
<cffunction name="delproxy" access="public" returntype="void">
	<cfquery datasource="#DB#">
	delete from auction_userbidproxy where aid = #aid#
	</cfquery>
</cffunction>


<!---旧出价无效--->
<cffunction name="setisbid0" access="public" returntype="void">
	<cfquery datasource="#DB#">
		update auction_userbid set is_bid = 0 where aid = #aid# and is_bid = 1
	</cfquery>
	<cfquery datasource="#DB#">
		update auction_userlastbid set is_bid = 0 where aid = #aid# and is_bid = 1
	</cfquery>
</cffunction>

<!---获取手机号--->
<cffunction name="getuidmobile" access="public" returntype="query">
<cfquery datasource="#DB#" name="getuidmobile">
SELECT mobile
FROM user_base
WHERE uid = #uid#
</cfquery>
<cfreturn getuidmobile>
</cffunction>

</cfcomponent>